<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Item extends BaseController {
    public function __construct()
    {

        parent::__construct();
        $this->load->model('item_model', 'item');
        $this->load->model('ad_model', 'ad');
    }
    /**
     *------------------------------------------------------------------------------------------------------------------
     * @title：根据分类id获取产品列表
     * @action：/Item/index.json
     * @method：GET
     * @param：cat_id             类型：INT，必须：YES ，描述：分类id
     * @author:：KevinBai
     *------------------------------------------------------------------------------------------------------------------
     */
    /**
     *------------------------------------------------------------------------------------------------------------------
     * @title：首页新闻接口（轮播图、图文新闻、视频新闻）
     * @action：/news/index
     * @method：GET
     * @author:：KevinBai
     *------------------------------------------------------------------------------------------------------------------
     */

      public function index(){
        $limit = 4;
        $adList = $this->ad->getAdList();
        foreach ($adList as $key => $value) {
            $adList[$key]['imgurl'] = BASEURL.$value['imgurl'];
            unset($adList[$key]['isDel']);
        }
        $texturesList = $this->item->get_item_list_by_catid(array(),"itemid,title,categoryid,addtime,referPrice",0,$limit);
        foreach ($texturesList as $key => $value) {
            $cover = $this->item->get_default_cover($value['itemid']);
            $texturesList[$key]['cover'] = $cover ? UPLOADURL.$cover : '';
            $texturesList[$key]['addtime'] = humanDate($value['addtime']);
        }
        $totalData = array(
            'adList'=>$adList,
            'recommentItem'=>$texturesList,
            );
        $this->createApi($totalData); exit();
    }

     public function itemList(){
		$params['per_page'] = $this->queryData['per_page'];
		$params['cat_id'] = $this->queryData['cat_id'];
		$params['sort'] = $this->queryData['sort'];
        $validate = new Validate([
            'per_page' => 'require|number',
			'cat_id' => 'require|number',
			'sort' => 'require|number',
        ]);
        if (!$validate->check($params)) {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
        $cateInfo = $this->item->get_cat_by_id($params['cat_id']);
        unset($cateInfo['addtime'],$cateInfo['onlinestatus'],$cateInfo['imgurl'],$cateInfo['sort_order']);
        $limit  = $this->config->item('limit');
        $fields = " itemid,title,categoryid,addtime";
        $moreItemList = array();
        $moreItemList = $this->item->get_item_list_by_catid(array("categoryid"=>$params['cat_id'],'sort'=>$params['sort']), $fields,$per_page,$limit);
        foreach ($moreItemList as $key => $value) {
                    $imgurl = $this->item->get_default_cover($value['itemid']);
                    $moreItemList[$key]['imgurl'] = $imgurl ? UPLOADURL.$imgurl : "";
                }
        $data = array('cateInfo'=>$cateInfo,'itemList'=>$moreItemList);
        $this->createApi($data);
    }


/**
     *------------------------------------------------------------------------------------------------------------------
     * @title：根据分类id获取更多
     * @action：/Item/moreItemList.json
     * @method：GET
     * @param：cat_id             类型：INT，必须：YES ，描述：分类id
     * @author:：KevinBai
     *------------------------------------------------------------------------------------------------------------------
     */    
    public function detail(){
		$params['itemid'] = $this->queryData['itemid'];
        $validate = new Validate([
            'itemid' => 'require|number',
        ]);
        if (!$validate->check($params)) {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }

        $itemInfo = $this->item->get_item_by_id($params['itemid'], " itemid,title,summary,historystory,referPrice ");
       
        unset($itmeInfo['isDel']);
        unset($itmeInfo['onlinestatus']);
        unset($itmeInfo['addtime']);
        $itemInfo['summary'] = str_replace('/upload/',BASEURL.'upload/',$itemInfo['summary']);
        $itemInfo['historystory'] = str_replace('/upload/',BASEURL.'upload/',$itemInfo['historystory']);

	  //获取产品封面轮播图
        $itemCover = $this->item->get_img_list_byid($itemid, "imgurl");
        if($itemCover){
            foreach ($itemCover as $key => $value) {
                $itemCover[$key]['imgurl'] = $value['imgurl'] ? UPLOADURL.$value['imgurl'] : "";
            }
        }
        if(!empty($itmeInfo)){
            $itmeInfo['cover'] = $itemCover;
        }
        $this->createApi($itemInfo);
    }
   
}

/* End of file category.php */
/* Location: ./application/controllers/luckydog/category.php */
